<?php
/**
 * @see Epic_View_Block
 */
require 'Epic/View/Block.php';

/**
 * @category   Epic
 * @package    Epic_Controller
 * @author     Epic Core Team <nguyendnhat@gmail.com>
 */
class Epic_Controller_Action
{	
	/**
     * Request object
     * @var Epic_Controller_Request_Abstract
     */
    private $_request = null;
  
	/**
     * Block layout object
     * @var Epic_View_Block
     */
	private $_layout = null;
	
	/**
     * Allow render layout
     * @var boolean
     */
	public $noRender = false;
	
	protected $_helper;
    
    /**
     * Class constructor
     *
     * The request objects should be registered with the controller, as should be any
     * additional optional arguments; these will be available via {@link getRequest()}.
     *
     * After the request are set, the {@link $_helper helper} is initialized.
     *
     * Finally, {@link init()} is called as the final action of instantiation, 
     * and may be safely overridden to perform initialization tasks;
     * as a general rule, override {@link init()} instead of the
     * constructor to customize an action controller's instantiation.
     * 
     * @param Epic_Controller_Request_Abstract $request
     * @return void
     */
	public function __construct(Epic_Controller_Request_Abstract $request)
	{	
		$this->_request = $request;
		$this->_helper = new Epic_Helper_Redirector();
		$this->init();
	}
	
    /**
     * Initialize object
     *
     * Called from {@link __construct()} as final step of object instantiation.
     *
     * @return void
     */
    public function init(){}
	
    /**
     * Retrieve request object
     *
     * @return Epic_Controller_Request_Abstract
     */
    public function getRequest()
    {
        return $this->_request;
    }
  	
    /**
     * Load layout
     */
    private function _loadLayout()
    {
    	$requestData = array('moduleName'     => $this->getRequest()->getModuleName(),
    		  				 'controllerName' => $this->getRequest()->getControllerName(),
    		  				 'actionName' 	  => $this->getRequest()->getActionName());
    						
		$blockLayout = new Epic_Simplexml_Layout($requestData);
		$this->_layout = $blockLayout->generateLayout();
    }
    
    protected function _redirect($url)
    {
        return $this->_helper->redirector($url);
    }
    
    /**
     * Destructor
     * 
     * Render layout
     */
	public function __destruct()
	{	
		if(!$this->noRender){
			$this->_loadLayout();
			echo $this->_layout->render();
		}
	}
}
